package com.easemob.helpdesk.widget.chatrow;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.recyclerview.widget.RecyclerView;

import com.easemob.helpdesk.adapter.ChatAdapter;
import com.easemob.helpdesk.adapter.UserChatAdapter;
import com.easemob.helpdesk.bean.MsgChat;
import com.easemob.helpdesk.utils.DateUtils;
import com.housekeeperapp.R;

import java.util.Date;

/**
 * Created by wsj on 2018/5/22.
 */

public abstract class BaseMsgViewHolder extends RecyclerView.ViewHolder{

    protected static final String TAG = "viewholder";
    public ImageView ivAvatar;
    public TextView timestamp;
    public ImageView ivStatus;
    public Activity activity;
    public Context context;
    public UserChatAdapter adapter;
    private View itemView;

    public BaseMsgViewHolder(Activity activity, UserChatAdapter chatAdapter, View itemView) {
        super(itemView);

        this.itemView = itemView;
        this.activity = activity;
        context = activity;
        adapter = chatAdapter;
        initView();
    }

    private void initView(){
        ivAvatar = (ImageView) itemView.findViewById(R.id.iv_userhead);
        timestamp = (TextView) itemView.findViewById(R.id.timestamp);
        ivStatus = (ImageView) itemView.findViewById(R.id.msg_status);
        onFindViewById();
    }

    public final void handleMessage(MsgChat message, final int position){
        handleViewMessage(message, position);
        String curTime = message.getSendTime();
        long curLongTime;
        if(curTime.length() == 13){
            curLongTime =  Long.parseLong(curTime);
        }else{
            curLongTime =  Integer.parseInt(curTime) * 1000L;
        }

        if (position == 0) {
            timestamp.setText(DateUtils.getTimestampString(new Date(curLongTime)));
            timestamp.setVisibility(View.VISIBLE);
        } else {
            // 两条消息时间离得如果稍长，显示时间
            MsgChat prevMessage = adapter.getItem(position - 1);
            String prevTime = prevMessage.getSendTime();
            long prevLongTime;
            if(prevTime.length() == 13){
                prevLongTime =  Long.parseLong(prevTime);
            }else{
                prevLongTime =  Integer.parseInt(prevTime) * 1000L;
            }

            if (prevMessage != null && DateUtils.isCloseEnough(curLongTime, prevLongTime)) {
                timestamp.setVisibility(View.GONE);
            } else {
                timestamp.setText(DateUtils.getTimestampString(new Date(curLongTime)));
                timestamp.setVisibility(View.VISIBLE);
            }
        }
    }

    public abstract void handleViewMessage(MsgChat message, final int position);

    /**
     *
     */
    protected abstract void onFindViewById();

    /**
     * 通过xml查找相应的ID，通用方法
     * @param id
     * @param <T>
     * @return
     */
    protected <T extends View> T findViewById(@IdRes int id) {
        return (T) itemView.findViewById(id);
    }


}
